<?php

 class ControllerUDEMCatalogFood extends Controller {
	 public function index(){
		require_once(DIR_SYSTEM . 'library/m/mobilepage.php');
        
        $page_limit = $this->config->get('config_admin_limit');
	 	$this->load->model('ude/catalog/food');
        
		if(isset($this->request->get['page'])){
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
        
		$condition = array(
			'start'    => ($page - 1) * $page_limit,
			'limit'    => $page_limit
		);
		
		$foods = $this->model_ude_catalog_food->getFoods($condition);
	    $total = $this->model_ude_catalog_food->getTotalFoodsNumber();
        
		$pagelist = new MobilePage();
		$pagelist->page = $page;
		$pagelist->itemsInPage = count($foods);
		$pagelist->setTotalNumber($total, $page_limit);
        
		$i = 0;
		foreach($foods as $food){
            $food_specials = $this->model_ude_catalog_food->getFoodSpecials($food['food_id']);
		
			foreach ($food_specials  as $food_special) {
				if (($food_special['date_start'] == '0000-00-00' || $food_special['date_start'] < date('Y-m-d')) && ($food_special['date_end'] == '0000-00-00' || $food_special['date_end'] > date('Y-m-d'))) {
					$special = $food_special['price'];
			
					break;
				}					
			}
            
			$data = array(
				'id'   		    => (int) $food['food_id'],
				'name' 		    => $food['name'],
				'price' 	    => (float) $food['price'],
				'image' 	    => $food['image'],
				'sortOrder'    => (int) $food['sort_order'],
				'special' 	    => $special,
                'dateModified' => $food['date_modified']
			);
			
			$pagelist->items[$i] = $data;
            
            $special = -1;
			$i ++;
		}
		
		echo toJson($pagelist);
	 }
 }
 ?>